package com.chd.driversystem.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.chd.driversystem.service.DriverSystemService;
import com.chd.driversystem.util.tools.Constants;
import com.chd.driversystem.util.tools.ActivityCollector;
import com.chd.driversystem.util.tools.LogUtil;
import com.chd.driversystem.util.tools.MyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import butterknife.ButterKnife;

/**
 * Created by feiyu on 17-5-9.
 */

public class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";
    private Context context = this;
    protected Map<String,Object> params;

    private ForceOfflineReceiver receiver;

    protected DriverSystemService.SystemBindler systemBindler;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            systemBindler = (DriverSystemService.SystemBindler) service;
            switch (context.getClass().getSimpleName()){
                case DriverActivity.TAG:
                    systemBindler.setWhere(DriverSystemService.DRIVERACTIVITY);
                    break;
                case EvaluationActivity.TAG:
                    systemBindler.setWhere(DriverSystemService.EVALUATIONACTIVITY);
                    break;
                case LoginActivity.TAG:
                    systemBindler.setWhere(DriverSystemService.LOGINACTIVITY);
                    break;
                case ManagerActivity.TAG:
                    systemBindler.setWhere(DriverSystemService.MANAGERACTIVITY);
                    break;
                case RankingActivity.TAG:
                    systemBindler.setWhere(DriverSystemService.RANKINGACTIVITY);
                    break;
                default:
                    break;
            }
            systemBindler.setParams(params);
            systemBindler.refreshNetData();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.d(TAG, getClass().getSimpleName());
        ActivityCollector.addActivity(this);
        Intent intent = new Intent(this,DriverSystemService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.ACTION_FORCE_OFFLINE);
        intentFilter.addAction(Constants.ACTION_LOGOUT);
        receiver = new ForceOfflineReceiver();
        registerReceiver(receiver, intentFilter);
        Intent intent = new Intent(this,DriverSystemService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
        unbindService(connection);
    }

    class ForceOfflineReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(final Context context, Intent intent) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("警告");
            if (intent.getAction().equals(Constants.ACTION_FORCE_OFFLINE)){
                builder.setMessage("您被强制下线，请从新登录！");
                builder.setCancelable(false);
            }else if (intent.getAction().equals(Constants.ACTION_LOGOUT)){
                builder.setMessage("是否退出登录");
                builder.setCancelable(true);
            }
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCollector.finishAll();
                    LoginActivity.actionStart(context);
                }
            });
            builder.show();
        }
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
}
